package com.czwj.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.Company;
import com.czwj.entities.DataGridData;
import com.czwj.entities.Duty2;
import com.czwj.entities.Duty3;
import com.czwj.entities.Duty4;
import com.czwj.entities.Quantities;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.PlanDaily;
import com.czwj.entities.Quantities;
import com.czwj.entities.Role;
import com.czwj.entities.UserInfo;
import com.czwj.service.Duty2Service;
import com.czwj.service.Duty3Service;
import com.czwj.service.Duty4Service;
import com.czwj.service.DutyChild2Service;
import com.czwj.service.DutyChild3Service;
import com.czwj.service.QuantitiesService;
import com.czwj.util.CipherUtil;
import com.czwj.util.TextUtil;

@Controller
public class QuantitiesAction{
	@Resource
	QuantitiesService quantitiesService;
	
	@RequestMapping(value = "quantitiess",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Quantities> quantitiess(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) String projectitemid,
			@RequestParam(value = "projectitemchildid",required = false) String projectitemchildid,
			@RequestParam(value = "startdate",required = false) String startdate,
			@RequestParam(value = "enddate",required = false) String enddate){
		DataGridData<Quantities> result = new DataGridData<>();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(projectitemid)){
			map.put("projectitemid",projectitemid);
		}
		if(TextUtil.isNotEmpty(projectitemchildid)){
			map.put("projectitemchildid",projectitemchildid);
		}
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate",startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate",enddate);
		}
		List<Quantities> list = quantitiesService.search(map);
		result.setRows(list);
		result.setTotal(quantitiesService.count(map));
		return result;
	}
	
	@RequestMapping(value = "/quantitieslist",method = RequestMethod.POST)
	@ResponseBody
	public List<Quantities> quantitieslist(
			@RequestParam(value = "projectitemid",required = false) String projectitemid){
		Map<String,Object> map = new HashMap<String,Object>();
		if(TextUtil.isNotEmpty(projectitemid)){
			map.put("projectitemid",projectitemid);
		}
		List<Quantities> list = quantitiesService.search(map);
		return list;
	}
	
	@SystemControllerLog(description = "修改工程量")
	@RequestMapping(value = "/QuantitiesSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult quantitiesSave(@RequestBody Quantities item,HttpServletRequest request) throws Exception{
		OperateResult result = new OperateResult();
		int count = 0;
		if(item.getId() != null && item.getId() > 0){
			count = quantitiesService.update(item);
		}else{
			Subject currentUser = SecurityUtils.getSubject();
			item.setCreaterid(((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			item.setCreatedate(new Date());
			item.setCreateip(request.getRemoteAddr());
			count = quantitiesService.insert(item);
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "删除工程量")
	@RequestMapping(value = "QuantitiesDelete.do",method = RequestMethod.DELETE)
	@ResponseBody
	public OperateResult QuantitiesDelete(@RequestParam(value = "id",required = true) Integer id){
		OperateResult result = new OperateResult();
		int count = quantitiesService.delete(id);
		result.setSuccess(count > 0);
		return result;
	}
	
	@RequestMapping(value = "/reviewed/quantities.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Quantities> reviewedQuantities(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "startdate",required = false) String startdate,
			@RequestParam(value = "enddate",required = false) String enddate,
			@RequestParam(value = "projectitemid",required = false) String projectitemid){
		DataGridData<Quantities> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate",startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate",enddate);
		}
		if(TextUtil.isNotEmpty(projectitemid) && !projectitemid.equals("0")){
			map.put("projectitemid",projectitemid);
		}
		map.put("status","4");
		result.setRows(quantitiesService.searchbylevel2(map));
		result.setTotal(quantitiesService.countbylevel2(map));
		return result;
	}
	
	@RequestMapping(value = "/review/quantities.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Quantities> unreviewedQuantities(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) String projectitemid){
		DataGridData<Quantities> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(TextUtil.isNotEmpty(projectitemid) && !projectitemid.equals("0")){
			map.put("projectitemid",projectitemid);
		}
		map.put("status",6);
		result.setRows(quantitiesService.searchbylevel2(map));
		result.setTotal(quantitiesService.countbylevel2(map));
		return result;
	}
	
	@RequestMapping(value = "/QuantitiesReviewed.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Quantities> QuantitiesReviewed(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows){
		DataGridData<Quantities> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(userInfo.getRoleid() == 1 || userInfo.getRoleid() == 2){
			
		}else if(userInfo.getRoleid() == 3){
			map.put("status","3,4");
			result.setRows(quantitiesService.searchReviewedLevel2(map));
			result.setTotal(quantitiesService.countReviewedLevel2(map));
		}
		return result;
	}
}
